package yazan.com.pdacollector_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.maning.mndialoglibrary.MProgressDialog;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yazan.com.pdacollector_android.R;
import yazan.com.pdacollector_android.base.BaseActivity;
import yazan.com.pdacollector_android.base.Presenter;
import yazan.com.pdacollector_android.presenter.AccountPresenter;
import yazan.com.pdacollector_android.utils.MProgressDialogHelper;
import yazan.com.pdacollector_android.utils.PDAGlobal;

public class LoginActivity extends BaseActivity {
    //<editor-fold desc=”成员变量”>


    @BindView(R.id.userNameEditText)
    EditText userNameEditText;
    @BindView(R.id.passwordEditText)
    EditText passwordEditText;
    @BindView(R.id.serverEditText)
    EditText serverEditText;
    @BindView(R.id.loginButton)
    Button loginButton;
    @BindView(R.id.checkBox)
    CheckBox checkBox;
    @BindView(R.id.imageViewLogo)
    ImageView imageViewLogo;
    @BindView(R.id.changePassWordButton)
    Button changePassWordButton;


    //</editor-fold>


    //<editor-fold desc=”生命周期”>


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        configUI();
    }


    //</editor-fold>

    //<editor-fold desc=”私有方法”>

    private void configUI() {
        userNameEditText.setText(SPUtils.getInstance().getString("username", ""));
        passwordEditText.setText(SPUtils.getInstance().getString("password", ""));
        serverEditText.setText(SPUtils.getInstance().getString("server", ""));
    }

    @OnClick({R.id.checkBox, R.id.loginButton, R.id.changePassWordButton})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.checkBox: {
                if (!checkBox.isChecked()) {
                    SPUtils.getInstance().remove("username");
                    SPUtils.getInstance().remove("password");
                    SPUtils.getInstance().remove("server");
                } else {
                    String userName = userNameEditText.getText().toString();
                    String passWord = passwordEditText.getText().toString();
                    String server = serverEditText.getText().toString();
                    SPUtils.getInstance().put("username", userName);
                    SPUtils.getInstance().put("password", passWord);
                    SPUtils.getInstance().put("server", server);
                }
            }
            break;
            case R.id.loginButton: {
                String msg = validationField();
                if (msg == null) {
                    if (checkBox.isChecked()) {
                        MProgressDialogHelper.showLoading(this);
                        String userName = userNameEditText.getText().toString();
                        String passWord = passwordEditText.getText().toString();
                        String server = serverEditText.getText().toString();
                        SPUtils.getInstance().put("username", userName);
                        SPUtils.getInstance().put("password", passWord);
                        SPUtils.getInstance().put("server", server);
                        String serverStr = serverEditText.getText().toString();
                        if (serverStr.contains("http")) {
                            PDAGlobal.baseUrl = serverStr;
                        } else {
                            PDAGlobal.baseUrl = "http://" + serverStr;
                        }
                        AccountPresenter.login(PDAGlobal.baseUrl, userName, passWord, "", 1, new LoginListener(this));
                    }
                } else {
                    ToastUtils.showShort(msg);
                }
            }
            break;
            case R.id.changePassWordButton: {
                jumpToChangePassWord();
            }
            break;
        }
    }

    /**
     * 验证表单
     *
     * @return .
     */
    private String validationField() {
        if (StringUtils.isEmpty(userNameEditText.getText())) {
            return "账号不能为空";
        }
        if (StringUtils.isEmpty(passwordEditText.getText())) {
            return "密码不能为空";
        }
        if (StringUtils.isEmpty(serverEditText.getText())) {
            return "服务器地址不能为空";
        }
        return null;
    }


    private void jumpToMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void jumpToChangePassWord() {
        if (StringUtils.isEmpty(serverEditText.getText())) {
            ToastUtils.showShort("服务器地址不能为空!");
            return;
        }

        Intent intent = new Intent(this, ChangePassWordActivity.class);
        intent.putExtra("username", userNameEditText.getText().toString());
        intent.putExtra("baseurl", serverEditText.getText().toString());
        startActivity(intent);
    }


    //</editor-fold>

    //<editor-fold desc=”内部类”>

    /**
     * 登录回调
     */
    private static class LoginListener implements Presenter.PresenterListener<HashMap> {

        private WeakReference<LoginActivity> weakReference;

        public LoginListener(LoginActivity context) {
            this.weakReference = new WeakReference<>(context);
        }

        @Override
        public void onSuccess(HashMap object) {
            MProgressDialog.dismissProgress();
            final boolean flag = (boolean) object.get("Flag");
            if (weakReference.get() != null) {
                if (flag) {
                    MProgressDialogHelper.showSuccess(weakReference.get(), "登录成功", new MProgressDialogHelper.MProgressDialogHelperListener
                            () {
                        @Override
                        public void complete() {
                            if (weakReference.get() != null) {
                                weakReference.get().jumpToMain();
                            }
                        }
                    });
                } else {
                    MProgressDialogHelper.showFail(weakReference.get(), (String) object.get("Message"), null);
                }
            }
        }

        @Override
        public void onError(String error) {
            MProgressDialog.dismissProgress();
            if (weakReference.get() != null) {
                MProgressDialogHelper.showFail(weakReference.get(), "网络链接异常!", null);
            }
        }
    }

    //</editor-fold>


}
